*Change working directory
cd "/Users/gehlbach/Dropbox/Projects/Handbook paper/Data"
clear matrix
clear
set mem 3000m
set matsize 2000


/* LOAD DATA */

*full4.dta:
  *linearly interpolates capital stock from adjacent years for any firm missing that variable in 1996, 1997, or 2002, regardless of region
  *linearly interpolates employment from adjacent years for firms in region 1163 in 2000
  *interpolates capital stock for all firms in 2005 as capital stock in 2004

use full4, clear

*drop outliers

gen cap = exp(logcap)
gen emp = exp(logemp)
gen out = exp(lsale)
*
foreach x of varlist cap emp out {
* 
by okpo, sort: gen last=1 if `x'~=. & f.`x'==.
by okpo, sort: gen first=1 if `x'~=. & l.`x'==.
* 
by okpo: gen out1=1 if `x'/l.`x'>5 & f.`x'/`x'<0.2 & `x'~=. & l.`x'~=. & f.`x'~=.
by okpo: gen out2=1 if `x'/l.`x'<0.2 & f.`x'/`x'>5 & `x'~=. & l.`x'~=. & f.`x'~=.
by okpo: gen out3=1 if f.`x'/`x'>10 & `x'~=. & f.`x'~=. & first==1 & f.out1~=1 & f.out2~=1 &  f2.`x'~=.
by okpo: gen out4=1 if f.`x'/`x'<0.1 & `x'~=. & f.`x'~=. & first==1 & f.out1~=1 & f.out2~=1 &  f2.`x'~=.
by okpo: gen out5=1 if `x'/l.`x'>10 & `x'~=. & l.`x'~=. & last==1 & l.out1~=1 & l.out2~=1 &  l2.`x'~=.
by okpo: gen out6=1 if `x'/l.`x'<0.1 & `x'~=. & l.`x'~=. & last==1 & l.out1~=1 & l.out2~=1 &  l2.`x'~=.
* 
gen outlier`x'=1 if out1==1 | out2==1 | out3==1 | out4==1 | out5==1 |out6==1
drop out1 out2 out3 out4 out5 out6
drop last first
}
*
drop if outliercap == 1
drop if outlieremp == 1
drop if outlierout == 1

*drop missing values (do this after dropping outliers so that can take advantage of time series where exists)

drop if missing(year,ter,lsale,logemp,logcap,ind,ldo,lfo,lho,lmo)


/* VARIABLE CONSTRUCTION BEFORE DETRENDING */

*year dummies
tab year, gen(y)
local j=1
forval i=1/21 {
local j=`i'+84
rename y`i' y`j'
label variable y`j' `""'
}

*industry-year dummies
foreach i of numlist 85/105 {
forv j = 1/10 {
gen ind`j'_y`i' = ind`j'*y`i'
}
}

*ldo*year interactions (no privatizations until 1993)
foreach i of numlist 94/105 {
gen ldo_y`i' = ldo*y`i'
}

*lfo*year interactions
foreach i of numlist 94/105 {
gen lfo_y`i' = lfo*y`i'
}

save priv_aggregate_dynamics, replace


/* DETRENDING */

tsset okpo year
DETREND lsale logemp logcap ldo* lfo* e1-e10 c1-c10 y85-y105, by(okpo) time(year)
rename _DTlsale dtlsale
rename _DTlogcap dtlogcap
rename _DTlogemp dtlogemp
rename _DTldo dtldo
forv i = 94/105 {
rename _DTldo_y`i' dtldo_y`i'
}
rename _DTlfo dtlfo
forv i = 94/105 {
rename _DTlfo_y`i' dtlfo_y`i'
}
forv i = 1/10 {
rename _DTe`i' dte`i'
}
forv i = 1/10 {
rename _DTc`i' dtc`i'
}
foreach i of numlist 85/105 {
rename _DTy`i' dty`i'
}

save dtwork_aggregate_dynamics, replace


/* VARIABLE CONSTRUCTION AFTER DETRENDING */

*dt_industry*year interactions
foreach i of numlist 85/105 {
forv j = 1/10 {
gen dtind`j'_y`i' = ind`j'*dty`i'
}
}

save interwork_aggregate_dynamics, replace 


/* DYNAMICS */

*ssc install parmest

use interwork_aggregate_dynamics, clear
reg dtlsale dtlfo_y94-dtlfo_y105 dtldo_y94-dtldo_y105 dte* dtc* dtind*_y*, cluster(okpo) noc 
parmest, format(estimate min95 max95) saving(temp, replace)
*domestic dynamics
use temp, clear
keep in 13/24
keep estimate min95 max95 
rename estimate dynamics
matrix year = [1994\1995\1996\1997\1998\1999\2000\2001\2002\2003\2004\2005]
svmat year
rename year1 year
twoway scatter dynamics year in 1/12, connect(l) mcolor(navy) lc(navy) title("") xlabel(1994(1)2005) xtitle("") ytitle(Estimated domestic privatization effect) ylabel(-.2(.1).6, grid gmin gmax) || rspike max95 min95 year, lc(navy) legend(off)
graph export dynamics.pdf, replace
twoway scatter dynamics year in 1/12, connect(l) title("") xlabel(1994(1)2005) xtitle("") ytitle(Estimated domestic privatization effect) ylabel(-.2(.1).6, grid gmin gmax) || rspike max95 min95 year, legend(off) scheme(s2mono)
graph export dynamics_bw.pdf, replace
*foreign dynamics
use temp, clear
keep in 1/12
keep estimate min95 max95 
rename estimate dynamics
matrix year = [1994\1995\1996\1997\1998\1999\2000\2001\2002\2003\2004\2005]
svmat year
rename year1 year
twoway scatter dynamics year in 1/12, connect(l) mcolor(navy) lc(navy) title("") xlabel(1994(1)2005) xtitle("") ytitle(Estimated foreign privatization effect) ylabel(-.2(.1).6, grid gmin gmax) || rspike max95 min95 year, lc(navy) legend(off)
graph export foreigndynamics.pdf, replace
twoway scatter dynamics year in 1/12, connect(l) title("") xlabel(1994(1)2005) xtitle("") ytitle(Estimated foreign privatization effect) ylabel(-.2(.1).6, grid gmin gmax) || rspike max95 min95 year, legend(off) scheme(s2mono)
graph export foreigndynamics_bw.pdf, replace

